package com.vegetable.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <Servlet相关工具>
 * @author ZhangYang
 * @date 2021/4/22 15:26
 * @version 1.0.0
 */
public class HttpServletUtils {

    /**
     * <获取 HttpServletRequest>
     * @return javax.servlet.http.HttpServletRequest
     * @author ZhangYang
     * @date 2021/4/22 15:28
     * @version 1.0.0
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    /**
     * <获取 HttpServletResponse>
     * @return javax.servlet.http.HttpServletResponse
     * @author ZhangYang
     * @date 2021/4/22 15:28
     * @version 1.0.0
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getResponse();
    }

    /**
     * <获取httpsession>
     * @return javax.servlet.http.HttpSession
     * @author ZhangYang
     * @date 2021/4/22 15:42
     * @version 1.0.0
     */
    public static HttpSession getHttpSession() {
        HttpServletRequest request = getRequest();
        if(request!=null){
            return request.getSession();
        }
        return null;
    }

    /**
     * <获取客户端ip地址>
     * @return java.lang.String
     * @author ZhangYang
     * @date 2021/4/22 15:42
     * @version 1.0.0
     */
    public static String getClientIpAddr() {
        HttpServletRequest request = getRequest();
        if(request==null){
            return "";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if(ip.contains(",")){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
